import React from "react";
import {
  Link,
  Routes,
  Route,
  useNavigate,
  useLocation,
} from "react-router-dom";

const Home = () => {
  // 6版本删除了useHistory
  // 新增了useNavigate
  const navigate = useNavigate();

  // console.log(navigate);

  const fn = () => {
    // 如果navigate函数里面直接写路径，就相当于push
    // navigate("/about");

    // 如果加上{ replace: true }，就相当于repalce的跳转
    // navigate("/about", { replace: true });

    // 直接传数字，表示前进或者后退
    // navigate(2)
    // navigate(-1)

    navigate(
      {
        pathname: "/about",
        search: "?a=3&b=4",
        hash: "#hhh",
      },
      {
        state: {
          x: 10,
        },
        replace: true,
      }
    );
  };

  return (
    <div>
      这是首页 - <button onClick={fn}>btn</button>
    </div>
  );
};
const About = () => {
  const location = useLocation();
  console.log(location);
  return <div>这是关于页</div>;
};

const App = () => {
  return (
    <>
      <h2>router-v6-basic</h2>
      <ul>
        <li>
          <Link to="/">首页</Link>
        </li>
        <li>
          <Link to="/about">关于页</Link>
        </li>
      </ul>

      <hr />

      <Routes>
        <Route path="/" element={<Home />}></Route>
        <Route path="/about" element={<About />}></Route>
      </Routes>
    </>
  );
};

export default App;
